﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ToursAgencyDAL;

namespace TourAgency
{
    public partial class Offers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["searchResult"] != null)
                {
                    this.gridViewTourOffers.DataSource = (IQueryable)Session["searchResult"];
                    this.gridViewTourOffers.DataBind();
                    Session["searchResult"] = null;
                }
                else
                {
                    this.gridViewTourOffers.DataSource = ToursAgencyDataFuncs.GetAllTours();
                    this.gridViewTourOffers.DataBind();
                }
            }
        }

        protected string EncodeEval(string property)
        {
            string currentProperty = (string)Eval(property);
            string result = Server.HtmlEncode(currentProperty);
            return result;
        }

        protected string GetDateOnly(string property)
        {
            DateTime date = (DateTime)Eval(property);
            string result = date.ToShortDateString();
            return result;
        }

        protected void ShowDetails(object sender, CommandEventArgs e)
        {
            int tourID = int.Parse(e.CommandArgument.ToString());
            var tour = ToursAgencyDataFuncs.GetTourByIDAsIqueriable(tourID);
            this.listViewDetails.DataSource = tour;
            this.listViewDetails.DataBind();

            var hotelFacs = ToursAgencyDataFuncs.GetAllHotelFacilitiesByTourID(tourID);
            this.listViewHotelFacilities.DataSource = hotelFacs;
            this.listViewHotelFacilities.DataBind();
        }

        protected void MakeReservation(object sender, CommandEventArgs e)
        {
            int tourID = int.Parse(e.CommandArgument.ToString());
            Session["currentTour"] = ToursAgencyDataFuncs.GetTourByID(tourID);
            var tour = ToursAgencyDataFuncs.GetTourByIDAsIqueriable(tourID);
            this.listViewDetails.DataSource = tour;
            this.listViewDetails.DataBind();

            var hotelFacs = ToursAgencyDataFuncs.GetAllHotelFacilitiesByTourID(tourID);
            this.listViewHotelFacilities.DataSource = hotelFacs;
            this.listViewHotelFacilities.DataBind();

            this.gridViewTourOffers.Visible = false;
            this.tablePersonInformation.Visible = true;

            this.labelPageInfo.Text = "Please enter information about the people...";
            this.labelTotalPrice.Visible = true;
            this.textBoxNotes.Visible = true;
            this.buttonConfirmReservation.Visible = true;
        }

        protected void ConfirmReservation(object sender, EventArgs e)
        {
            Tour tourReservation = (Tour)Session["currentTour"];
            DataTable attendees = (DataTable)ViewState["people"];
            Reservation newReservation = new Reservation();
            newReservation.TourID = tourReservation.ID;
            newReservation.TotalPrice = attendees.Rows.Count * tourReservation.Price;
            newReservation.PaidPrice = 0m;
            newReservation.Notes = this.textBoxNotes.Text;
            newReservation.Status = "Unconfirmed";
            Random r = new Random();
            string refNumber = tourReservation.ID.ToString() + r.Next(1000000).ToString() + tourReservation.StartDate.ToShortDateString() + tourReservation.EndDate.ToShortDateString();
            newReservation.ReferenceNumber = refNumber;
            ToursAgencyDataFuncs.CreateReservation(newReservation);

            int reservationID = ToursAgencyDataFuncs.GetReservationIDBYRefNumber(refNumber);
            for (int i = 0; i < attendees.Rows.Count; i++)
            {
                Person p = new Person();
                p.FirstName = attendees.Rows[i]["First Name"].ToString();
                p.LastName = attendees.Rows[i]["Last Name"].ToString();
                p.PassportNumber = attendees.Rows[i]["Passport"].ToString();
                p.ReservationID = reservationID;
                ToursAgencyDataFuncs.CreatePerson(p);
            }

            this.gridViewTourOffers.Visible = true;
            this.tablePersonInformation.Visible = false;

            this.labelPageInfo.Text = "Here is a list of all tours";
            this.labelTotalPrice.Text = "Current price: 0.00";
            this.labelTotalPrice.Visible = false;
            this.textBoxNotes.Visible = false;
            this.buttonConfirmReservation.Visible = false;
            this.gridViewAddedPeople.Visible = false;
            ViewState["people"] = null;
        }

        protected void AddPersonToList(object sender, EventArgs e)
        {
            if (ViewState["people"] == null)
            {
                CreateViewStateTable();
            }

            DataTable currentPeople = (DataTable)ViewState["people"];
            DataRow dr = currentPeople.NewRow();
            dr["First Name"] = this.textBoxFirstName.Text;
            dr["Last Name"] = this.textBoxLastName.Text;
            dr["Passport"] = this.textBoxPassport.Text;
            currentPeople.Rows.Add(dr);
            this.gridViewAddedPeople.DataSource = currentPeople;
            this.gridViewAddedPeople.DataBind();
            ViewState["people"] = currentPeople;
            this.gridViewAddedPeople.Visible = true;
            this.textBoxPassport.Text = "";
            this.textBoxLastName.Text = "";
            this.textBoxFirstName.Text = "";
            Tour tour = (Tour)Session["currentTour"];
            decimal totalPrice = currentPeople.Rows.Count * tour.Price;
            this.labelTotalPrice.Text = "Current Total Price: " + totalPrice.ToString();
        }

        protected void RemovePerson(object sender, EventArgs e)
        {
            GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
            int rowIndex = gvRow.RowIndex;
            DataTable currentPeople = (DataTable)ViewState["people"];
            currentPeople.Rows.RemoveAt(rowIndex);
            this.gridViewAddedPeople.DataSource = currentPeople;
            this.gridViewAddedPeople.DataBind();
            ViewState["people"] = currentPeople;
            Tour tour = (Tour)Session["currentTour"];
            decimal totalPrice = currentPeople.Rows.Count * tour.Price;
            this.labelTotalPrice.Text = "Current Total Price: " + totalPrice.ToString();
        }

        private void CreateViewStateTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("First Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Last Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Passport", typeof(string)));
            ViewState["people"] = dt;
        }

        protected string GetDestinationByID(string property)
        {
            int id = (int)Eval(property);
            string destinationName = ToursAgencyDataFuncs.GetDestinationNameByID(id);
            string encodedDestination = Server.HtmlEncode(destinationName);
            return encodedDestination;
        }

        protected string GetTransportnByID(string property)
        {
            int id = (int)Eval(property);
            string transportName = ToursAgencyDataFuncs.GetTransportNameByID(id);
            string encodedTransport = Server.HtmlEncode(transportName);
            return encodedTransport;
        }

        protected string GetBoardByID(string property)
        {
            int id = (int)Eval(property);
            string boardName = ToursAgencyDataFuncs.GetBoardNameByID(id);
            string encodedBoard = Server.HtmlEncode(boardName);
            return encodedBoard;
        }
    }
}